package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.ActorDao;
import cn.edu.sdjzu.xg.bysj.dao.StudentDao;
import cn.edu.sdjzu.xg.bysj.domain.Student;
import cn.edu.sdjzu.xg.bysj.domain.StudentClass;
import cn.edu.sdjzu.xg.bysj.domain.User;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import cn.edu.sdjzu.xg.bysj.security.UserDao;
import cn.edu.sdjzu.xg.bysj.util.Condition;
import cn.edu.sdjzu.xg.bysj.util.JdbcHelper;
import cn.edu.sdjzu.xg.bysj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class StudentService {
	private static final StudentDao studentDao = StudentDao.getInstance();
	private static final StudentService studentService = new StudentService();

	private StudentService() {
	}

	public static StudentService getInstance() {
		return studentService;
	}

	/**
	 * 一些方法使用了事务，所以即使仅测试dao也应从Service中开始。
	 *
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException, BysjException {
		Connection connection = JdbcHelper.getConn();
		StudentClass studentClass = StudentClassService.getInstance().find(1);

		Student t1 = new Student("name05", "5", true, null, studentClass);
		System.out.println(t1);
//		StudentService.getInstance().add(t1);

		Student s2 = StudentService.getInstance().find(8);

		System.out.println("s2 = " + s2);
	}

	public Collection<Student> findAll() throws SQLException, BysjException {
		return this.findAll(null, null);
	}

	public Collection<Student> findAll(List<Condition> conditionList_json_str, Pagination pagination) throws SQLException, BysjException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		Collection<Student> students = studentDao.findAll(connection, conditionList_json_str, pagination);
		JdbcHelper.close(connection);
		return students;
	}

	public Student find(Integer id) throws SQLException, BysjException {
		//获得connection资源
		Connection connection = JdbcHelper.getConn();
		Student student = studentDao.find(connection,id);
		//关闭connection资源
		JdbcHelper.close(connection);
		return student;
	}

	public boolean update(Student student) throws SQLException {
		return studentDao.update(student);
	}

	public boolean delete(Integer id) throws SQLException, BysjException {
		Student student = this.find(id);
		return studentDao.delete(student);
	}

	public boolean delete(Student student) throws SQLException {
		//如果该学生有关联的课题，不能删除
//		if(student.getProjects().size()>0){
//			return false;
//		}
		return studentDao.delete(student);
	}

	/**
	 * 添加Student对象，该对象id的值取自它超类对象的id值
	 *
	 * @param student 要添加的Student对象
	 * @return 添加成功返回true，否则false
	 * @throws SQLException
	 */
	public boolean add(Student student) throws SQLException {
		//获得进行事务的连接，添加学生和添加用户都使用本连接对象
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		boolean added = false;
		try {
			//添加actor对象，返回值为自动生成的id
			int actorId = ActorDao.getInstance().add(connection);
			//为student的id赋值
			student.setId(actorId);
			//studentDao添加学生对象，无返回值
			studentDao.add(student, connection);
			//创建用户对象，密码和用户名取自student字段值no
			User user = new User(0, student.getNo(), student.getNo(),  student);
			//增加用户
			added = UserDao.getInstance().add(user, connection);
			//提交事务
			connection.commit();
		} catch (SQLException e) {
			//回滚事务中所有操作
			e.printStackTrace();
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null, connection);
		}
		return added;
	}
}
